

EFFECT.Mat = Material( "sprites/bluelaser1" )
EFFECT.Mat2 = Material( "effects/tool_tracer" )
EFFECT.matLight = Material( "sprites/gmdm_pickups/light" )
EFFECT.matRefraction = Material( "egon_ringbeam" )

/*---------------------------------------------------------
   Init( data table )
---------------------------------------------------------*/
function EFFECT:Init( data )

	self.texcoord = math.Rand( 0, 20 )/3
	self.Position = data:GetStart()
	self.WeaponEnt = data:GetEntity()
	self.Attachment = data:GetAttachment()
	
	// Keep the start and end pos - we're going to interpolate between them
	self.StartPos = self:GetTracerShootPos( self.Position, self.WeaponEnt, self.Attachment )
	self.EndPos = data:GetOrigin()
	
	// This determines a bounding box - if the box is on screen we get drawn
	// You pass the 2 corners of the bounding box
	// It doesn't matter what order you pass them in - I sort them for you in the engine
	// We want to draw from start to origin
	// These Vectors are in entity space
	self.Entity:SetCollisionBounds( self.StartPos -  self.EndPos, Vector( 110, 110, 110 ) )
	self.Entity:SetRenderBoundsWS( self.StartPos, self.EndPos, Vector()*8 )
	
	self.StartPos = self:GetTracerShootPos( self.Position, self.WeaponEnt, self.Attachment )
	
	self.Alpha = 255
	self.FlashA = 255
	
		local emitter = ParticleEmitter( self.StartPos )
	
		for i=0, 80 do
		
		local Low = self.EndPos
		local High = self.StartPos
		LAngle = (High - Low):Angle()
		local Normal = LAngle:Forward()
		local SpeedMax = 180
		local SpeedMin = -180
		local Splode = Vector(math.Rand(SpeedMin,SpeedMax),math.Rand(SpeedMin,SpeedMax),math.Rand(SpeedMin,SpeedMax))
		
			local vPos = Low
			local vVel = Splode//- vPos
			local particle = emitter:Add( "sprites/gmdm_pickups/light", vPos )
			if (particle) then
				particle:SetVelocity( vVel )
				particle:SetLifeTime( 0 )
				particle:SetDieTime( .6 )
				particle:SetStartAlpha( 255 )
				particle:SetEndAlpha( 0 )
				particle:SetStartSize( 10 )
				particle:SetEndSize( 30 )
				particle:SetRoll( math.Rand(0, 360) )
				particle:SetRollDelta( 0 )
			end
			
			local particle = emitter:Add( "effects/Spark", vPos )
			if (particle) then
				particle:SetVelocity( vVel )
				particle:SetLifeTime( 0 )
				particle:SetDieTime( .6 )
				particle:SetStartAlpha( 100 )
				particle:SetEndAlpha( 0 )
				particle:SetStartSize( 10 )
				particle:SetEndSize( 0 )
				particle:SetRoll( math.Rand(0, 360) )
				particle:SetRollDelta( 0 )
			end
			
		end
		
	emitter:Finish()
	
end

/*---------------------------------------------------------
   THINK
---------------------------------------------------------*/
function EFFECT:Think( )

	self.FlashA = self.FlashA - 650 * FrameTime()
	if (self.FlashA < 0) then self.FlashA = 0 end

	self.Alpha = self.Alpha - 250 * FrameTime()
	if (self.Alpha < 0) then return false end
	
	return true

end

/*---------------------------------------------------------
   Draw the effect
---------------------------------------------------------*/
function EFFECT:Render( )
	
	self.Length = (self.StartPos - self.EndPos):Length()
	
	local texcoord = self.texcoord
		
	render.SetMaterial( self.matRefraction )
	self.matRefraction:SetMaterialFloat( "$refractamount", self.Alpha / 2200 )
	render.UpdateRefractTexture()
	
		render.DrawBeam( self.StartPos, 										// Start
					 self.EndPos,											// End
					 0 + (1-(self.Alpha/255))*60,													// Width
					 texcoord,														// Start tex coord
					 texcoord + self.Length / 512,									// End tex coord
					 Color( 255, 255, 255, self.Alpha ) )		// Color (optional)

	render.SetMaterial( self.Mat )
		render.DrawBeam( self.StartPos, 										// Start
					 self.EndPos,											// End
					 8,													// Width
					 texcoord,														// Start tex coord
					 texcoord + self.Length / 256,									// End tex coord
					 Color( 255, 255, 255, math.Clamp(self.Alpha, 0,255)) )		// Color (optional)'
					 
		render.SetMaterial( self.Mat2 )
		render.DrawBeam( self.StartPos, 										// Start
					 self.EndPos,											// End
					 0 + (1-(self.Alpha/255))*60,													// Width
					 texcoord,														// Start tex coord
					 texcoord + self.Length / 256,									// End tex coord
					 Color( 255, 255, 255, math.Clamp(self.Alpha, 0,80)) )		// Color (optional)
					 
	render.SetMaterial( self.matLight )
	render.DrawSprite( self.StartPos, 128, 128, Color( 255, 255, 255, self.FlashA ) )

end
